package com.starblink.brand.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.brand.adapter.FilterCategoryCell
import com.starblink.brand.adapter.FilterCategoryTitleCell
import com.starblink.brand.adapter.FilterEndCell
import com.starblink.brand.adapter.FilterTextCell
import com.starblink.brand.bean.FilterCategoryBean
import com.starblink.brand.bean.FilterCategoryTitleBean
import com.starblink.brand.databinding.*
import com.starblink.brand.model.BrandCollectionVM
import com.starblink.brand.widget.FilterDialogI

class FilterCategoryFragment : BaseTVMFragment<FragmentFilterCategoryBinding, BrandCollectionVM>() ,
    FilterCategoryI {

    companion object {
        fun obtainFragment(brandCollectionId:String?,frontCategoryIds:ArrayList<String>?): FilterCategoryFragment {
            val filterCategoryFragment= FilterCategoryFragment()
            val bundle=Bundle()
            brandCollectionId?.let { bundle.putString("brandCollectionId",it) }
            frontCategoryIds?.let { bundle.putSerializable("frontCategoryIds",it) }
            filterCategoryFragment.arguments=bundle
            return filterCategoryFragment
        }
    }
    private var filterI: FilterDialogI?=null
    lateinit var rvFilterLayoutManager:LinearLayoutManager

    //一级分类
    private val categoryAdapter by lazyOnNone {
        MutableAdapter(viewModel.filterContentList.value!!).apply {
            addVhDelegate {
                FilterCategoryTitleCell(CellFilterCategoryTitleBinding.inflate(layoutInflater,it,false))
            }
            addVhDelegate {
                FilterCategoryCell(CellFilterCategoryBinding.inflate(layoutInflater,it,false),this@FilterCategoryFragment)
            }
            addVhDelegate {
                FilterEndCell(CellTheEndBinding.inflate(layoutInflater,it,false))
            }
        }
    }
    //字母
    private val letterAdapter by lazyOnNone {
        MutableAdapter(viewModel.filterLetterList.value!!).apply {
            addVhDelegate {
                FilterTextCell(CellFilterLetterBinding.inflate(layoutInflater,it,false),this@FilterCategoryFragment)
            }
        }
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentFilterCategoryBinding {
        return FragmentFilterCategoryBinding.inflate(inflater, container, false)
    }

    override fun startObserve() {
        viewModel.filterContentList.observe(this) {
            categoryAdapter.notifyDataSetChanged()
        }
        viewModel.filterLetterList.observe(this){
            letterAdapter.notifyDataSetChanged()
        }
        viewModel.filterCategoryListResult.observe(this) {
            //是否有一级分类
            if(viewModel.filterCategoryList.isEmpty()){
                filterI?.hiddenCategoryFragment()
            }
            if(viewModel.filterContentList.value!!.size>0) {
                viewModel.changeLetterState(0, viewModel.filterContentList.value!!.size - 1)
            }
        }
    }

    override fun initData() {
        rvFilterLayoutManager=LinearLayoutManager(requireContext())
        viewBinding.rvFilterCategory.layoutManager = rvFilterLayoutManager
        viewBinding.rvFilterCategory.adapter = categoryAdapter
        viewBinding.rvFilterZm.layoutManager= LinearLayoutManager(requireContext())
        viewBinding.rvFilterZm.adapter=letterAdapter

        //监听滑动事件
        viewBinding.rvFilterCategory.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                //获取首个展示和最后一个展示项
                val firstVisibleItem =
                    rvFilterLayoutManager.findFirstCompletelyVisibleItemPosition()
                val lastVisibleItem =
                    rvFilterLayoutManager.findLastCompletelyVisibleItemPosition()
                //修改字母状态 不需要联动
//                viewModel.changeLetterState(firstVisibleItem,lastVisibleItem)

            }
        })
        val frontCategoryIds=arguments?.getStringArrayList("frontCategoryIds")
        val brandCollectionId=arguments?.getString("brandCollectionId")?:""
        viewModel.getFilterCategoryData(brandCollectionId,frontCategoryIds)
        viewModel.getFilterLetter()

    }

    //改变Done按钮状态
    override fun changeBtnStates() {
        this.filterI?.changeBtnStates()
        categoryAdapter.notifyDataSetChanged()
    }
    //滚动到指定的letter位置
    override fun scrollToLetter(letter: Char) {
        viewModel.filterContentList.value!!.forEachIndexed {index,letterItem->
            if(letterItem is FilterCategoryTitleBean &&  letterItem.key.code== letter.code){
                //平顺滑动 但会在底部
//                viewBinding.rvFilterCategory.smoothScrollToPosition(index)
                //指定项滑动到顶部
                rvFilterLayoutManager.scrollToPositionWithOffset(index, 0)
                return
            }
        }
    }

    fun setFilterDialogI(filterDialogI: FilterDialogI){
        this.filterI=filterDialogI
    }

    fun getFilterCategoryList():List<FilterCategoryBean>{
        //获取选中的类别
        return viewModel.filterCategoryList.filter{ it.ifChecked }
    }
    fun clearFilterCategoryList(){
        //清除选中的类别
        viewModel.filterCategoryList.map{ it.ifChecked=false }
        categoryAdapter.notifyDataSetChanged()
    }
}

interface FilterCategoryI{
    fun changeBtnStates() //按钮状态修改
    fun scrollToLetter(letter:Char)//点击Letter后滚动到指定位置
}